package com.starblink.android.common.web

import androidx.viewbinding.ViewBinding
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.CollectProductEvent
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.android.basic.util.bus.bean.RemoveStoresEvent
import com.starblink.basic.log.SKLogger
import com.starblink.web.WebBaseActivity
import com.starblink.web.bean.JsBridgeBean

/**
 * 所有WebView基类
 * **/
abstract class GuangWebBaseActivity<T : ViewBinding,VM : BaseViewModel> : WebBaseActivity<T, VM>(){
    private val TAG="GuangWebView"

    override fun initObservable() {
        super.initObservable()
        //同步登录状态
        FlowBus.with<Boolean>(FlowConst.LOGIN_STATUS_CHANGE).register(this) {
            SKLogger.d(TAG,"initObservable --- ${FlowConst.LOGIN_STATUS_CHANGE}")
            mWebView.onActivityResult(
                FlowConst.LOGIN_STATUS_CHANGE,
                JsBridgeBean(data = mapOf("loginResult" to UserDataCenter.isLogin())).toJson())
        }
        //同步收藏状态
        FlowBus.with<CollectProductEvent>(FlowConst.COLLECT_PRODUCT_EVENT).register(this) { event ->
            SKLogger.d(TAG,"initObservable --- ${FlowConst.COLLECT_PRODUCT_EVENT}")
            mWebView.onActivityResult(FlowConst.COLLECT_PRODUCT_EVENT,mapOf("productId" to event.id, "isCollected" to event.collect).toJson())
        }
        //同步订阅数据
        FlowBus.with<FollowStoreEvent>(FlowConst.FOLLOW_STORE_EVENT).register(this) { event ->
            SKLogger.d(TAG,"initObservable --- ${FlowConst.FOLLOW_STORE_EVENT}")
            mWebView.onActivityResult(FlowConst.FOLLOW_STORE_EVENT,mapOf("id" to event.id, "subOrNot" to event.followed).toJson())
        }
        //批量取消订阅
        FlowBus.with<RemoveStoresEvent>(FlowConst.REMOVE_STORE_EVENT).register(this) { event ->
            SKLogger.d(TAG,"initObservable --- ${FlowConst.REMOVE_STORE_EVENT}")
            mWebView.onActivityResult(FlowConst.REMOVE_STORE_EVENT,mapOf("ids" to event.ids).toJson())
        }
    }

}